/**
 * 
 */
package com.arx.common;

/**
 * @author Donald
 */
public class Common {
	/** Maximum value for an object of type char. */
	public static final short	CHAR_MAX	= 127;
	/** Minimum value for an object of type char. */
	public static final short	CHAR_MIN	= -127;
	/** the singleton instance. */
	private static Common		instance;
	/** Maximum value for an object of type long int. */
	public static final int		LONG_MAX	= 2147483647;
	/** Minimum value for an object of type long int. */
	public static final int		LONG_MIN	= -2147483647;
	/** Maximum value for an object of type unsigned char. */
	public static final short	UCHAR_MAX	= 255;
	/** Maximum value for an object of type unsigned long int. */
	public static final long	ULONG_MAX	= 4294967295L;
	/**
	 * Gets the one and only instance of {@link Common}.
	 * @return {@link Common}
	 */
	public static Common getInstance() {
		if (Common.instance == null) {
			Common.instance = new Common();
		}
		return Common.instance;
	}
	/** Hidden constructor. */
	private Common() {}
	/**
	 * Checks a floating-point value to see if it falls into the range for a C++
	 * unsigned char value (0-255).
	 * @param val the floating-point value
	 * @return the closest floating point value within the 0-255 range
	 */
	public float checkCharValue(final float val) {
		float f = val;
		if (f < Common.CHAR_MIN) {
			f = Common.CHAR_MIN;
		}
		if (f > Common.CHAR_MAX) {
			f = Common.CHAR_MAX;
		}
		return f;
	}
	/**
	 * Checks a long value to see if it falls into the range for a C++ char
	 * value (-127 to 127).
	 * @param val the long value
	 * @return the closest long point value within the -127 to 127 range
	 */
	public long checkCharValue(final long val) {
		long l = val;
		if (l < Common.CHAR_MIN) {
			l = Common.CHAR_MIN;
		}
		if (l > Common.CHAR_MAX) {
			l = Common.CHAR_MAX;
		}
		return l;
	}
	/**
	 * Checks a long value to see if it falls into the range for a C++ long int
	 * value (-2147483647 to 2147483647).
	 * @param val the floating-point value
	 * @return the closest floating point value within the 0-255 range
	 */
	public float checkLongValue(final float val) {
		float f = val;
		if (f < Common.LONG_MIN) {
			f = Common.LONG_MIN;
		}
		if (f > Common.LONG_MAX) {
			f = Common.LONG_MAX;
		}
		return f;
	}
	/**
	 * Checks a floating-point value to see if it falls into the range for a C++
	 * unsigned char value (0-255).
	 * @param val the floating-point value
	 * @return the closest floating point value within the 0-255 range
	 */
	public long checkLongValue(final long val) {
		long l = val;
		if (l < Common.LONG_MIN) {
			l = Common.LONG_MIN;
		}
		if (l > Common.LONG_MAX) {
			l = Common.LONG_MAX;
		}
		return l;
	}
	/**
	 * Checks a floating-point value to see if it falls into the range for a C++
	 * unsigned char value (0-255).
	 * @param val the floating-point value
	 * @return the closest floating point value within the 0-255 range
	 */
	public float checkUnsignedCharValue(final float val) {
		float f = val;
		if (f < 0) {
			f = 0;
		}
		if (f > Common.UCHAR_MAX) {
			f = Common.UCHAR_MAX;
		}
		return f;
	}
	/**
	 * Checks a floating-point value to see if it falls into the range for a C++
	 * unsigned long value (0-4294967295).
	 * @param val the floating-point value
	 * @return the closest floating point value within the 0-4294967295 range
	 */
	public float checkUnsignedLongValue(final float val) {
		float f = val;
		if (f < 0) {
			f = 0;
		}
		if (f > Common.ULONG_MAX) {
			f = Common.ULONG_MAX;
		}
		return f;
	}

}
